<template>
  <div
    :class="[individual ? 'individual-wrapper' : '', 'bounceIn']"
    v-drag
    ref="selfElement"
  >
    <div
      :class="[
        'card-wrapper',
        'note-card',
        actived
          ? critical
            ? 'note-card-critical critical'
            : 'note-card-actived'
          : 'note-card-inactived',
        individual ? 'individual' : '',
      ]"
    >
      <div
        :class="['note-card-header', actived ? 'note-card-header-active' : '']"
      >
        <div>便签</div>
        <div>
          <el-button class="note-card-button" link @click="openEditor">
            <el-icon>
              <Edit />
            </el-icon>
          </el-button>
          <el-button
            v-if="actived"
            class="note-card-button"
            link
            @click="setIndividual"
          >
            <el-icon v-if="individual"><ZoomOut /></el-icon>
            <el-icon v-else><ZoomIn /></el-icon>
          </el-button>
        </div>
      </div>
      <div class="note-card-body" v-if="actived">
        <div
          v-for="note in criticalNotes"
          class="note-content note-content-critical"
        >
          <el-icon class="blinking note-content-icon">
            <BellFilled />
          </el-icon>
          <span>{{ note.text }}</span>
        </div>
        <div v-for="note in normalNotes" class="note-content">
          <el-icon class="note-content-icon">
            <Message />
          </el-icon>
          <span>{{ note.text }}</span>
        </div>
      </div>
      <Editor ref="editorWindow" @close="loadNotes(true)" />
    </div>
  </div>
</template>

<script setup>
import { h, ref, computed, onMounted, onUnmounted, nextTick } from "vue";
import axios from "axios";
import { ElMessage } from "element-plus";
import dayjs from "dayjs";
import Editor from "./editor.vue";

const actived = ref(false);
const critical = ref(false);
const individual = ref(false);

const editorWindow = ref();
const openEditor = () => {
  editorWindow.value.open(notes.value);
};

const selfElement = ref();
const setIndividual = () => {
  individual.value = !individual.value;
  if (individual.value) {
    selfElement.value.style.left = "calc(50vw - 120px)";
    selfElement.value.style.top = "180px";
  }
};

const notes = ref([]);
const criticalNotes = ref([]);
const normalNotes = ref([]);

const loadNotes = (mute = false) => {
  axios
    .get("api/global_notes_handler.php")
    .then((response) => {
      // console.log("GlobalNote:", response.data);
      if (response.data.success && response.data.result.length > 0) {
        notes.value = response.data.result.filter((note) =>
          dayjs().isBefore(note.until)
        );
      } else {
        notes.value = new Array();
        // console.warn("GlobalNoteWarn:", response.data);
        // mute ? null : ElMessage.warning("出错了~");
      }
      fetchNotes();
    })
    .catch((error) => {
      notes.value = new Array();
      console.error("GlobalNoteError:", error);
      // mute ? null : ElMessage.error("出错了~");
    });
};
const fetchNotes = () => {
  normalNotes.value = notes.value.filter((note) => note.type == "一般");
  criticalNotes.value = notes.value.filter((note) => note.type == "重要");
  if (notes.value.length > 0) {
    actived.value = true;
    if (criticalNotes.value.length > 0) {
      critical.value = true;
    } else {
      critical.value = false;
    }
  } else {
    actived.value = false;
  }
};

const autoRefreshTimer = ref(null);
const autoRefreshInterval = ref(3000);

onMounted(() => {
  loadNotes(true);
  autoRefreshTimer.value = setInterval(
    () => loadNotes(true),
    autoRefreshInterval.value
  );
});

onUnmounted(() => {
  clearInterval(autoRefreshTimer.value);
});
</script>

<style scoped>
@import "@/styles/card.css";
@import "@/styles/blinking.css";
@import "./criticalCardStyle.css";

.note-card {
  font-size: 12px;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
  text-align: left;
}

.note-card-inactived {
  background-color: #666;
  height: 30px;
}

.note-card-actived {
  background-color: #0099cc;
  /* background-color: #009966; */
  background-color: #2b90d9;
  color: #ffffff;
}

.note-card-critical {
  background-color: #ff6666;
  color: #ffffff;
}

.note-card-header-active {
  background-color: rgba(255, 255, 255, 0.25);
  border-radius: 12px 12px 0 0;
  /* border-bottom: 1px solid rgba(255, 255, 255, 0.25); */
}
.note-card-header {
  height: 30px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  text-align: left;
  padding: 0 12px;
  font-size: 14px;
}
.note-card-body {
  height: calc(100% - 30px);
  width: 100%;
}
.note-card-button {
  padding: 0;
  margin: 0 2px !important;
}

.note-content {
  display: flex;
  align-items: top;
  justify-content: left;
  padding: 4px 0;
  margin: 0 12px;
  font-size: 14px;
  border-top: 1px solid rgba(255, 255, 255, 0.25);
}

.note-content-icon {
  margin-right: 3px;
  margin-top: 3px;
}

.note-content:first-child {
  border: none;
}

.note-content-critical {
  /* font-size: 16px; */
  font-weight: bold;
}
</style>
